<template>
    <div class="category-detail-page">
        <el-card shadow="never">
            <el-form
                :model="form"
                label-position="right"
                label-suffix=":"
                label-width="140px"
                ref="form"
                :rules="rules">
                <el-form-item label="勋章课程章标题" prop="title">
                    <el-input v-model="form.title" placeholder="请输入勋章课程章标题" class="input-width"></el-input>
                </el-form-item>
                <el-form-item label="勋章课程章描述" prop="describe">
                    <el-input v-model="form.describe" placeholder="请输入勋章课程章描述" class="input-width"></el-input>
                </el-form-item>
            </el-form>
            <div class="text-center">
                <el-button type="primary" @click="onSave">保 存</el-button>
                <el-button @click="onBack">返 回</el-button>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    components: {
    },
    data() {
        return {
            id: '',
            form: {
                describe: '',
                title: '',
                merit_course_id: '',
            },
            rules: {
                title: [
                    { required: true, message: '请输入勋章课程章标题', trigger: 'blur' },
                ],
            },
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (!valid) return;
                const api = this.id ? 'put' : 'post';
                API[api]('merit_chapter', this.form).then(() => {
                    $ele.$message.success(`${this.id ? '编辑' : '新增'}成功`);
                    this.onBack();
                });
            });
        },

        fetchData() {
            if (this.id) {
                API.get(`merit_chapter/${this.id}`).then((res) => {
                    this.form = res;
                });
            }
        },

        onBack() {
            this.$router.back();
        },
    },
    mounted() {
        this.form.merit_course_id = this.$route.query.courseid;
        this.id = this.$route.params.id;
        this.fetchData();
    },
};
</script>

<style lang="scss" scoped>
.input-width {
    max-width: 300px;
    width: 100%;
}
</style>
